<?php

namespace App\Http\Imports\BaseData;

use App\Models\Company;
use App\Models\ProcessRequirements;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ProcessRequirementsImport implements ToCollection
    {
    /**
     * @param Collection $collection
     */
    public function endColumn() : string
        {
        // 返回你想要限制的列数
        return 'E';
        }
    public function collection(Collection $collection)
        {
        set_time_limit(0);
        $collection->filter(function ($v, $k)
            {
            if ($k == 0) {
                return;
                }
            $company_name = $v[0];
            $name         = $v[1];
            $createtime   = $v[2];
            $jgmaterial   = $v[3];
            $cumaterial   = $v[4];

            $company_count = Company::whereName($company_name)->count();
            if ($company_count > 1) {
                fail("{$company_name}客户存在重复！");
                }
            if ($company_count == 0) {
                fail("{$company_name}客户不存在！");
                }

            $comapny_m_id = Company::whereName($company_name)->first()->id;


            if (isEmpty(trim($name))) {
                fail("工艺内容不能为空！");
                }

            ProcessRequirements::create([
                'name'          => $name,
                'company_id'    => $comapny_m_id,
                'opencard_date' => $createtime,
                'jgmaterial'    => $jgmaterial,
                'cumaterial'    => $cumaterial,
            ]);
            });

        }
    }
